#!/bin/bash
#
# Copyright(c) 2012-2021 Intel Corporation
# SPDX-License-Identifier: BSD-3-Clause
#

# The line below specified that line under it should be used as the test's short description when launching test via run_tests script.
# The text should not be longer than 80 chars - if it is, the script will strip addititonal characters
# DESCRIPTION Eviction Policy - Type Selection

# The line below says that this test should be included in BVT - it will be launched, when we use "./run_tests -b"

# USE_IN_NIGHTLY

# Standard beginning for every test - get the main tests directory and
# link the cas_lib file for CAS API, then use "start_test $*" to pass params
# and do other necessary checks and setup.
TESTS_DIR="$(dirname $0)/../"
. $TESTS_DIR/cas_lib
start_test $*

CACHE_DEVICE_SIZE=300M
CORE_DEVICE_SIZE=600M
TEST_DEVICE=${DEVICE_NAME}1-1

#param1 - YES - WB mode, NO - WT mode
eviction_policy_init() {
    local L_WB=$1

    # Use CACHE_DEVICE and CORE_DEVICE provided by configuration file and remove partitions from those devices
    TARGET_DEVICE_OPTION="$CACHE_DEVICE" remove_partitions
    TARGET_DEVICE_OPTION="$CORE_DEVICE" remove_partitions

    # Create 1 primary partitions on CACHE_DEVICE
    TARGET_DEVICE_OPTION="$CACHE_DEVICE" PARTITION_SIZE_OPTION=$CACHE_DEVICE_SIZE PARTITION_IDS_OPTION="1" make_primary_partitions

    # Create 1 primary partitions on CORE_DEVICE
    TARGET_DEVICE_OPTION="$CORE_DEVICE" PARTITION_SIZE_OPTION=$CORE_DEVICE_SIZE PARTITION_IDS_OPTION="1" make_primary_partitions

    # Set WB mode
    if [ "YES" == "$L_WB" ]
    then
        CACHE_MODE_OPTION="wb"
        test_log_trace "Cache mode is WB"
    else
        CACHE_MODE_OPTION="wt"
       test_log_trace "Cache mode is WT"
    fi

    # Start cache on CACHE_DEVICE1
    CACHE_ID_OPTION="1" CACHE_DEVICE_OPTION="${CACHE_DEVICE}-part1" start_cache

    # Add a core device using CORE_DEVICE1
    CACHE_ID_OPTION="1" CORE_DEVICE_OPTION="${CORE_DEVICE}-part1" add_core
}

eviction_policy_flush() {
    CACHE_ID_OPTION="1" flush_cache
}

eviction_policy_deinit() {
    sleep 1

    # Remove the core device from cache
    CACHE_ID_OPTION="1" CORE_ID_OPTION="1" remove_core

    # Clean up after the test
    CACHE_ID_OPTION="1" stop_cache
}

#
# START TEST
#

test_log_start

run_cmd eviction_policy_init

run_cmd eviction_policy_deinit

run_cmd eviction_policy_init YES

run_cmd eviction_policy_deinit

test_log_stop

#
# END TEST
#

# Always return 0 at the end of the test - if at any point something has failed
# in the API functions, test will end and return a proper result.
# If you need to check other things during the test and end the test earlier, you
# should end the test using "end_test $retval" function
end_test 0
